using System;
using System.Text;
using WpfApp1.Models;

namespace WpfApp1.Services
{
    /// <summary>
    /// 数据处理服务类
    /// </summary>
    public class DataProcessingService
    {
        /// <summary>
        /// 处理有效的TCP响应数据
        /// </summary>
        /// <param name="response">TCP响应字节数组</param>
        /// <param name="serverIp">服务器IP</param>
        /// <param name="serverPort">服务器端口</param>
        /// <param name="command">发送的命令</param>
        /// <returns>处理后的数据记录</returns>
        public TcpDataRecord ProcessValidData(byte[] response, string serverIp, int serverPort, string command)
        {
            var record = new TcpDataRecord
            {
                ServerIp = serverIp,
                ServerPort = serverPort,
                Command = command,
                RawResponse = BitConverter.ToString(response),
                CreatedAt = DateTime.Now,
                IsValid = false
            };

            try
            {
                // 查找最后一个值为0x20的元素的位置
                int lastSpaceIndex = -1;
                for (int i = response.Length - 2; i >= 0; i--) // 从倒数第二个元素开始，排除最后一个元素
                {
                    if (response[i] == 0x20)
                    {
                        lastSpaceIndex = i;
                        break;
                    }
                }

                if (lastSpaceIndex == -1)
                {
                    record.ExtractedData = "未找到值为0x20的元素";
                    return record;
                }

                // 拷贝最后一个0x20元素之后的所有元素（除了最后一个）
                int startIndex = lastSpaceIndex + 1;
                int endIndex = response.Length - 1; // 排除最后一个元素
                int newArrayLength = endIndex - startIndex;

                if (newArrayLength <= 0)
                {
                    record.ExtractedData = "提取的数据长度为0";
                    return record;
                }

                byte[] extractedData = new byte[newArrayLength];
                Array.Copy(response, startIndex, extractedData, 0, newArrayLength);

                // 将字节数组转换为字符串
                string resultString = Encoding.ASCII.GetString(extractedData);

                record.ExtractedData = resultString;
                record.IsValid = true;

                return record;
            }
            catch (Exception ex)
            {
                record.ExtractedData = $"处理数据时发生错误: {ex.Message}";
                return record;
            }
        }

        /// <summary>
        /// 测试数据处理功能
        /// </summary>
        /// <returns>测试数据记录</returns>
        public TcpDataRecord TestDataProcessing()
        {
            // 测试数据：02 73 41 4E 20 54 41 72 65 61 64 54 61 67 44 61 74 61 20 31 20 32 30 20 45 32 38 30 31 31 43 30 32 30 30 30 31 33 44 42 31 45 35 42 30 33 38 32 30 30 30 30 30 30 30 30 03
            byte[] testData = new byte[] {
                0x02, 0x73, 0x41, 0x4E, 0x20, 0x54, 0x41, 0x72, 0x65, 0x61, 0x64, 0x54, 0x61, 0x67, 0x44, 0x61, 
                0x74, 0x61, 0x20, 0x31, 0x20, 0x32, 0x30, 0x20, 0x45, 0x32, 0x38, 0x30, 0x31, 0x31, 0x43, 0x30, 
                0x32, 0x30, 0x30, 0x30, 0x31, 0x33, 0x44, 0x42, 0x31, 0x45, 0x35, 0x42, 0x30, 0x33, 0x38, 0x32, 
                0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x03
            };

            return ProcessValidData(testData, "192.168.0.241", 2112, "sMN TAreadTagData 0 2 0 8 32");
        }
    }
}
